<?php
namespace Views;
/**
 * File holds class for generating account view (sign in, sign up etc)
 *
 * PHP version 5.3.5
 *
 * @category   CM
 * @package    Views
 * @subpackage -
 * @author     markus karileet <markuskarileet@hotmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @link       -
 */
class Account extends Page {
	private $_service;
	
	public function __construct($language) {
		$this->setUp($language, 0);
		$this->_service = new \Services\Account();
	}
	
	/**
	 * Method shows sign in form
	 */
	public function viewSignInForm() {
		$this->_content = $this->_smarty->fetch('signin.tpl');
		$this->display();
	}
	
	/**
	 * Method logs user in the application. When login is unsuccessful, message
	 * is shown and user is redirected to sign in page. When successful, user
	 * is redirected to ones companys analytics page
	 * @param array $post HTTP POST array
	 */
	public function login($post) {
		$user = $this->_service->authenticate($post);
		if (!$user) {
			$this->_messages = array('error' => $this->_translation['loginFailed']);
			$this->viewSignInForm();
		} else {
			$template = new \Entities\Business();
			$template->setAccount($user->getId());
			$business = $this->_service->select($template);
			$businessView = new Business($_SESSION['l']);
			$businessView->setMessage(array('success' => $this->_translation['loginSuccess']));
			$businessView->viewMyAnalytics($business->getId());
		}
	}
	
	public function logout() {
		$this->_service->clearUserData();
		$this->_messages = array('success' => $this->_translation['logoutSuccess']);
		//reset menus with setup
		$this->setUp($_SESSION['l'], 0);
		$this->viewWelcomePage();
	}
	
	public function viewSignUpForm() {
		
	}
}